import config from './config'

export function request (target, data, type) {
  const token =
      data !== null && typeof data.token !== 'undefined' && data.token !== null
        ? data.token
        : null
  return new Promise((resolve, reject) => {
    wx.request({
      url: config.BASE_URL + target,
      data: data,
      method: type,
      header: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/json;charset=UTF-8',
        Authorization: token
      },
      success: function (res) {
        resolve(res.data)
      },
      fail: function (res) {
        reject(res.data)
      }
    })
  })
}

export function formatTime (timestamp) {
  var date = new Date((timestamp + 1))
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  // var hour = date.getHours()
  // var minute = date.getMinutes()
  // var second = date.getSeconds()
  // return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')

  return [year, month, day].map(formatNumber).join('-')
}

function formatNumber (n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
